---OOSE SoSe 2011 Aufgabe 3
--@author Mickey Reiss <mickeyreiss@gmail.com> (HU:417884)

require 'lunit/lunit'
require 'lunit/lunit-console'
require 'lunit/lunit-tests'
require 'Game'

module("kalaha_tests",lunit.testcase, package.seeall)

function setup()
  game = GameCollection:create():startSilent("Kalaha")
end

function teardown()
  game = nil
end

function test_move()
  game.board.board = 
  { 32, 1, 1, 0, 0, 0, 0,
        0, 0, 0, 0, 1, 1, 0
  }
  assert_equal(game.board.board[2],1)
  game:move(2)
  assert_equal(game.board.board[2],2)
  assert_equal(game.board.board[13],1)
  game:move(5)
  assert_equal(game.board.board[13],2)
end

function test_player_wins()
  local winner, isGameOver
  for i,nr in ipairs({ 3, 6, 4, 1, 5, 3, 1, 1, 2, 1, 3, 4}) do
    nextPlayer, isGameOver = game:move(nr)
    assert_false(isGameOver)
  end
  nextPlayer, isGameOver = game:move(1)
  assert_true(isGameOver)

  winner = game:checkVictory()

  assert_equal(game.player2, winner, "Expected player B to win")
end

function test_draw()
  game.board.board = 
  {36/2,  0,0,0,0,0,0,
          0,0,0,0,0,0,  36/2}
  local winner, isGameOver = game:checkVictory()
  assert_nil(winner)
  assert_true(isGameOver)
end

function test_skip_opponent_store()
  game.board.board = 
  {0,  9,26,0,0,1,0,
	0,0,0,0,0,0,  0}
  game:move(1)
  local a,b = game:getScores()
  assert_equal(1, a)
  assert_equal(0, b)
end

function test_capture()
  game.board.board = 
  {0,   1,0,0,1,0,0,
	      1,0,1,0,0,0,  32}
  game:move(4)
  assert_equal(2, game:getScores(), "A score of zero menas a capture did not occur")
end

function test_otherside_capture()
  game.board.board = 
  {0,   3,1,0,0,0,0,
        0,0,0,0,1,0,   31}
  game:move(1)
  assert_equal(1, game:getScores(), "Player A score should be 1. 3 means that an opposite side capture occurred.")
end

---Capture where opponent's pit is empty
--In our variation, this is NOT a capture
function test_empty_capture()
  game.board.board = 
  {0,   1,0,0,1,0,0,
        1,0,0,0,0,0,  33}
  game:move(4)
  assert_equal(0, game:getScores(), "A score of zero means a capture did not occur")
end

function test_last_in_store_play_agian()
  game.board.board = 
  {0,   1,0,0,1,0,0,
	1,0,0,0,0,0,  33}
  local next, over = game:move(1)
  assert_equal('A', next, "Next player incorrect")
  assert_false(over, "Game should not be over")
  assert_equal(game.player1, game.currentPlayer)
end

function test_try_empty_pit()
  game.board.board = 
  {0,   1,0,0,0,0,0,
	1,0,0,0,0,0,  33}
  local next, over = game:move(2)
  assert_equal('A', next)
  assert_false(over)
end

local stats = lunit.main()
if stats.errors > 0 or stats.failed > 0 then
  os.exit(1)
end
